package com.bestcem.xm.member.service.mq.send.impl;

import cn.hutool.json.JSONUtil;
import com.bestcem.xm.common.core.config.mq.XmMessageProducer;
import com.bestcem.xm.common.core.constant.StreamMqBinderConstants;
import com.bestcem.xm.member.mq.message.*;
import com.bestcem.xm.member.service.mq.send.MemberMessageSendService;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author leilei.zang
 */
@Slf4j
@Component
public class MemberMessageSendServiceImpl implements MemberMessageSendService {

    private XmMessageProducer messageProducer;

    @Autowired
    public void setMessageProducer(XmMessageProducer messageProducer) {
        this.messageProducer = messageProducer;
    }

    /**
     * 联系人/发送分组规则修改
     */
    @Override
    public void groupConditionModSend(GroupingMessage message) {
        try {
            messageProducer.send(StreamMqBinderConstants.MEMBER_GROUP_CONDITION_MOD_OUT, message);
        } catch (JsonProcessingException e) {
            log.error("【联系人】发送分组规则修改消息失败. bindingName:{}, object:{}", StreamMqBinderConstants.MEMBER_GROUP_CONDITION_MOD_OUT, JSONUtil.toJsonStr(message), e);
        }
    }

    /**
     * 联系人创建、联系人更新-调度至分组
     **/
    @Override
    public void memberCreationUpdSend(MemberCreationUpdMessage message) {
        try {
            messageProducer.send(StreamMqBinderConstants.MEMBER_CREATION_UPD_OUT, message);
        } catch (JsonProcessingException e) {
            log.error("【联系人】发送联系人创建/更新消息失败. bindingName:{}, object:{}", StreamMqBinderConstants.MEMBER_CREATION_UPD_OUT, JSONUtil.toJsonStr(message), e);
        }
    }

    /**
     * 联系人本地缓存
     **/
    @Override
    public void memberDbSend(SourceCardMessage message) {
        try {
            messageProducer.send(StreamMqBinderConstants.MEMBER_DB_OUT, message);
        } catch (JsonProcessingException e) {
            log.error("【联系人】发布来源名片失败. bindingName:{}, object:{}", StreamMqBinderConstants.MEMBER_DB_OUT, JSONUtil.toJsonStr(message), e);
        }

    }

    /**
     * 刪除联系人
     **/
    @Override
    public void memberDeletedSend(MemberDeletedMessage message) {
        try {
            messageProducer.send(StreamMqBinderConstants.MEMBER_DELETE_OUT, message);
        } catch (JsonProcessingException e) {
            log.error("【联系人】推送删除的联系人失败. bindingName:{}, object:{}", StreamMqBinderConstants.MEMBER_DELETE_OUT, JSONUtil.toJsonStr(message), e);
        }

    }

    /**
     * 发送打标签消息
     **/
    @Override
    public void memberSliceSend(MemberSliceMessage message) {
        try {
            messageProducer.send(StreamMqBinderConstants.MEMBER_SLICE_OUT, message);
        } catch (JsonProcessingException e) {
            log.error("【联系人】发送打标签消息失败. bindingName:{}, object:{}", StreamMqBinderConstants.MEMBER_SLICE_OUT, JSONUtil.toJsonStr(message), e);
        }

    }

    /**
     * 联系人、发布微信同步信息
     */
    @Override
    public void memberWechatMemberScheduleSend(WechatMemberSyncMessage message) {
        try {
            messageProducer.send(StreamMqBinderConstants.MEMBER_WECHAT_MEMBER_SCHEDULE_OUT, message);
        } catch (JsonProcessingException e) {
            log.error("【联系人】发布微信同步信息失败. bindingName:{}, object:{}", StreamMqBinderConstants.MEMBER_WECHAT_MEMBER_SCHEDULE_OUT, JSONUtil.toJsonStr(message), e);
        }
    }

    /**
     * 联系人分组删除
     */
    @Override
    public void groupDeletedSend(GroupDeletedMessage message) {
        try {
            messageProducer.send(StreamMqBinderConstants.MEMBER_GROUP_DELETED_OUT, message);
        } catch (JsonProcessingException e) {
            log.error("【联系人】发送分组删除消息失败. bindingName:{}, object:{}", StreamMqBinderConstants.MEMBER_GROUP_DELETED_OUT, JSONUtil.toJsonStr(message), e);
        }
    }

    /**
     * 推送创建的联系人
     *
     * @param message memberMessage
     */
    @Override
    public void memberCreatedSend(MemberMessage message) {
        try {
            messageProducer.send(StreamMqBinderConstants.MEMBER_MEMBER_CREATED_OUT, message);
        } catch (JsonProcessingException e) {
            log.error("【联系人】推送创建的联系人失败. bindingName:{}, object:{}", StreamMqBinderConstants.MEMBER_MEMBER_CREATED_OUT, JSONUtil.toJsonStr(message), e);
        }
    }

    /**
     * 更新联系人-fanout
     */
    @Override
    public void memberUpdatedSend(MemberMessage message) {
        try {
            messageProducer.send(StreamMqBinderConstants.MEMBER_MEMBER_UPDATED_OUT, message);
        } catch (JsonProcessingException e) {
            log.error("【联系人】更新联系人发送mq异常. bindingName:{}, object:{}", StreamMqBinderConstants.MEMBER_MEMBER_UPDATED_OUT, JSONUtil.toJsonStr(message), e);
        }
    }

    @Override
    public void memberWechatMemberSyncSend(WechatMemberSyncMessage message) {
        try {
            messageProducer.send(StreamMqBinderConstants.MEMBER_WECHAT_MEMBER_SYNC_OUT, message);
        } catch (JsonProcessingException e) {
            log.error("【联系人】 发布微信同步信息失败. bindingName:{}, object:{}", StreamMqBinderConstants.MEMBER_WECHAT_MEMBER_SYNC_OUT, JSONUtil.toJsonStr(message), e);
        }
    }

    /**
     * 联系人/发布微信同步信息
     */
    @Override
    public void syncWechatAccountInfoSend(SyncWechatAccountInfoMessage message) {
        try {
            messageProducer.send(StreamMqBinderConstants.MEMBER_SYNC_WECHAT_ACCOUNT_INFO_OUT, message);
        } catch (JsonProcessingException e) {
            log.error("【联系人】 发布微信同步信息失败. bindingName:{}, object:{}", StreamMqBinderConstants.MEMBER_SYNC_WECHAT_ACCOUNT_INFO_OUT, JSONUtil.toJsonStr(message), e);
        }
    }

    /**
     * 监听联系人延迟队列消息/group member 重新分组
     */
    @Override
    public void memberRegroupMemberSend(MemberCreationUpdMessage memberCreationUpdMessage, Date delayDate) {
        try {
            messageProducer.sendDelay(StreamMqBinderConstants.MEMBER_REGROUP_MEMBER_OUT, memberCreationUpdMessage, delayDate);
        } catch (JsonProcessingException e) {
            log.error("【联系人】监听联系人延迟队列消息/group member 重新分组失败. bindingName:{}, object:{}", StreamMqBinderConstants.MEMBER_REGROUP_MEMBER_OUT, JSONUtil.toJsonStr(memberCreationUpdMessage), e);
        }
    }

    @Override
    public void memberRegroupMembersSend(GroupingMessage groupingMessage, Date delayDate) {
        try {
            messageProducer.sendDelay(StreamMqBinderConstants.MEMBER_REGROUP_MEMBERS_OUT, groupingMessage, delayDate);
        } catch (JsonProcessingException e) {
            log.error("【联系人】监听联系人延迟队列消息/group member 重新分组失败. bindingName:{}, object:{}", StreamMqBinderConstants.MEMBER_REGROUP_MEMBERS_OUT, JSONUtil.toJsonStr(groupingMessage), e);
        }
    }

    /**
     * 微信公众号同步打开与关闭/延时
     */
    @Override
    public void wechatMemberSyncDelaySend(WechatMemberSyncMessage message, Date delayDate) {
        try {
            messageProducer.sendDelay(StreamMqBinderConstants.MEMBER_WECHAT_SYNC_DELAY_OUT, message, delayDate);
        } catch (JsonProcessingException e) {
            log.error("【联系人】微信同步联系人(调度) 延迟队列 bindingName:{}, object:{}", StreamMqBinderConstants.MEMBER_WECHAT_SYNC_DELAY_OUT, JSONUtil.toJsonStr(message), e);
        }
    }

    /**
     * 微信同步联系人(调度) 延迟队列
     */
    @Override
    public void wechatMemberScheduleSyncDelay(WechatMemberSyncMessage message, Date delayDate) {
        try {
            messageProducer.sendDelay(StreamMqBinderConstants.MEMBER_WECHAT_SCHEDULE_SYNC_DELAY_OUT, message, delayDate);
        } catch (JsonProcessingException e) {
            log.error("【联系人】微信同步联系人(调度) 延迟队列 bindingName:{}, object:{}", StreamMqBinderConstants.MEMBER_WECHAT_SCHEDULE_SYNC_DELAY_OUT, JSONUtil.toJsonStr(message), e);
        }
    }

    /**
     * 联系人/延时队列/微信公众号同步打开与关闭信息
     */
    @Override
    public void wechatMemberSyncDelayDelaySend(WechatMemberSyncMessage message, Date delayDate) {
        try {
            messageProducer.sendDelay(StreamMqBinderConstants.MEMBER_WECHAT_SYNC_DELAY_DELAY_OUT, message, delayDate);
        } catch (JsonProcessingException e) {
            log.error("【联系人】延时队列/微信公众号同步打开与关闭信息 bindingName:{}, object:{}", StreamMqBinderConstants.MEMBER_WECHAT_SYNC_DELAY_DELAY_OUT, JSONUtil.toJsonStr(message), e);
        }
    }

    /**
     * 存联系人足迹
     */
    @Override
    public void footprintSaveSend(FootprintMessage message) {
        try {
            messageProducer.send(StreamMqBinderConstants.MEMBER_FOOT_PRINT_SAVE_OUT, message);
        } catch (JsonProcessingException e) {
            log.error("【联系人】延时队列/微信公众号同步打开与关闭信息 bindingName:{}, object:{}", StreamMqBinderConstants.MEMBER_FOOT_PRINT_SAVE_OUT, JSONUtil.toJsonStr(message), e);
        }
    }

    /**
     * 联系人/发送消息member.member_created_by_respondent_submit
     */
    @Override
    public void memberCreatedByRespondentSubmitSend(SequenceMessage message) {
        try {
            messageProducer.send(StreamMqBinderConstants.MEMBER_CREATED_BY_RESPONDENT_SUBMIT_OUT, message);
        } catch (JsonProcessingException e) {
            log.error("【联系人】 联系人/发送消息member.member_created_by_respondent_submit bindingName:{}, object:{}", StreamMqBinderConstants.MEMBER_CREATED_BY_RESPONDENT_SUBMIT_OUT, JSONUtil.toJsonStr(message), e);
        }
    }

    @Override
    public void startTicketSurveyRespondentSubmitSend(RespondentDataMessage message) {
        try {
            messageProducer.send(StreamMqBinderConstants.START_TICKET_SURVEY_RESPONDENT_SUBMIT_OUT, message);
        } catch (JsonProcessingException e) {
            log.error("【联系人】 联系人/发送消息ticket.survey.respondent.submit bindingName:{}, object:{}", StreamMqBinderConstants.START_TICKET_SURVEY_RESPONDENT_SUBMIT_OUT, JSONUtil.toJsonStr(message), e);
        }
    }

}
